package com.jumi.microservice.service.logistics.express;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jumi.microservice.common.utils.DateUtil;
import com.jumi.microservice.common.utils.HttpClientUtils;
import com.jumi.microservice.common.utils.MD5;
import com.jumi.microservice.dto.logistics.Logistics;
import com.jumi.microservice.dto.logistics.LogisticsSearchRequest;
import com.jumi.microservice.dto.logistics.LogisticsSearchResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author by mazhao
 * 百世快递
 * @Classname BestExpress
 * @Description TODO
 * @Date 2020/8/21 14:30
 */
@Service
public class YtoExpress extends ResponseController implements Express {

    private static final Logger log = LoggerFactory.getLogger(YtoExpress.class);

    @Resource
    HttpClientUtils httpClientUtils;

    /**
     * 测试环境
     *
     */
//    private static String url = "http://opentestapi.yto.net.cn/service/waybill_query/v1/X1FKbA";
//    private static String user_id = "YTOTEST";
//    private static String app_key = "sF1Jzn";
//    private static String secret_key = "1QLlIZ";
//    private static String v = "1.01";

    /**
     * 正式环境
     */
    private static String url = "http://openapi.yto.net.cn/service/waybill_query/v1/X1FKbA";
    private static String user_id = "X1FKbA";
    private static String app_key = "sGPi15";
    private static String secret_key = "lknU0j";
    private static String v = "1.01";
    private static String method = "yto.Marketing.WaybillTrace";


    @Override
    public String expressCode() {
        return "YTO";
    }

    @Override
    public LogisticsSearchResponse logisticsSearch(LogisticsSearchRequest request) {
        log.info("圆通快递查询参数[{}]", JSON.toJSONString(request));
        JSONArray jsonArray = new JSONArray();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("Number",request.getLogisticsNo());
            String timestamp = DateUtil.formatDate(new Date(),"yyyy-mm-dd HH:mm:ss");
            String sign = secret_key+"app_key"+app_key+"formatJSONmethod"+method+"timestamp"
                    +timestamp+"user_id"+user_id+"v"+v;
            sign = MD5.getInstance().getMD5String(sign).toUpperCase();
            String params = "sign="+sign+"&app_key="+app_key+"&format=JSON&method="+method+"&timestamp="
                    +timestamp+"&user_id="+user_id+"&v="+v+"&param="+ URLEncoder.encode(jsonObject.toJSONString(),"UTF-8");
            Map<String, String> headers = new HashMap<>(1);
            headers.put("Content-Type", "application/x-www-form-urlencoded");
            String res = httpClientUtils.post(url, params, headers);
            log.info("请求返回结果[{}]",res);
            jsonArray = JSONObject.parseArray(res);
        } catch (Exception e) {
            log.error("圆通快递查询出错[{}]",e.getMessage());
        }
        LogisticsSearchResponse logisticsSearchResponse = checkResult(jsonArray,request);
        return controller(logisticsSearchResponse,jsonArray,"processInfo","upload_Time");
    }

}
